﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace sy4_7
{
    public partial class Form1 : Form
    {
        private int rocketSpeed = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadBackGround();
            InitRocket();
            InitProgress();
        }

        private void LoadBackGround()
        {
            try
            {
                Bitmap backPic = new Bitmap(@"..\..\Resources\发射场.jpg");
                this.BackgroundImage = backPic;
                this.BackgroundImageLayout = ImageLayout.Stretch;
                this.ClientSize = new Size(backPic.Width, backPic.Height);
                this.DoubleBuffered = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载背景图片失败: " + ex.Message);
                CreateTestBackground();
            }
        }

        private void InitRocket()
        {
            try
            {
                Bitmap pic = new Bitmap(@"..\..\Resources\火箭.gif");
                pictureBox1.Image = pic;
                pictureBox1.BackColor = Color.Transparent;
                pictureBox1.Size = new Size(pic.Width, pic.Height);
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

                pictureBox1.Left = this.ClientSize.Width / 2 - pictureBox1.Width / 2;
                pictureBox1.Top = this.ClientSize.Height - pictureBox1.Height + 100;
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载火箭图片失败: " + ex.Message);
                CreateTestRocket();
            }
        }

        private void InitProgress()
        {
            progressBar1.Dock = DockStyle.Top;
            progressBar1.Height = 20;
            progressBar1.Maximum = pictureBox1.Top + pictureBox1.Height;
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;
            progressBar1.Style = ProgressBarStyle.Continuous;
        }

        private void CreateTestBackground()
        {
            Bitmap testBg = new Bitmap(800, 600);
            using (Graphics g = Graphics.FromImage(testBg))
            {
                Rectangle rect = new Rectangle(0, 0, 800, 600);
                using (System.Drawing.Drawing2D.LinearGradientBrush brush =
                    new System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.DarkBlue, Color.Black, 90f))
                {
                    g.FillRectangle(brush, rect);
                }

                Random rand = new Random();
                for (int i = 0; i < 100; i++)
                {
                    int x = rand.Next(800);
                    int y = rand.Next(600);
                    g.FillEllipse(Brushes.White, x, y, 2, 2);
                }

                g.FillRectangle(Brushes.Gray, 350, 400, 100, 200);
                g.DrawString("发射场", new Font("微软雅黑", 16), Brushes.White, new PointF(360, 450));
            }
            this.BackgroundImage = testBg;
            this.ClientSize = new Size(800, 600);
        }

        private void CreateTestRocket()
        {
            Bitmap testRocket = new Bitmap(50, 100);
            using (Graphics g = Graphics.FromImage(testRocket))
            {
                g.Clear(Color.Transparent);

                Point[] rocketBody = {
                    new Point(25, 0),
                    new Point(45, 80),
                    new Point(35, 100),
                    new Point(15, 100),
                    new Point(5, 80)
                };
                g.FillPolygon(Brushes.Silver, rocketBody);

                g.FillEllipse(Brushes.LightBlue, 15, 20, 20, 20);

                Point[] flame = {
                    new Point(15, 100),
                    new Point(10, 120),
                    new Point(25, 110),
                    new Point(40, 120),
                    new Point(35, 100)
                };
                g.FillPolygon(Brushes.OrangeRed, flame);
            }
            pictureBox1.Image = testRocket;
            pictureBox1.BackColor = Color.Transparent;
            pictureBox1.Size = new Size(50, 100);

            pictureBox1.Left = this.ClientSize.Width / 2 - pictureBox1.Width / 2;
            pictureBox1.Top = this.ClientSize.Height - pictureBox1.Height + 100;
        }

        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                InitRocket();
                progressBar1.Value = 0;
                timer1.Start();
                this.Text = "火箭发射程序 - 发射中...";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Top -= rocketSpeed;

            if (progressBar1.Value < progressBar1.Maximum)
            {
                progressBar1.Value += rocketSpeed;
            }

            if (pictureBox1.Top + pictureBox1.Height <= 0)
            {
                timer1.Stop();
                this.Text = "火箭发射程序 - 发射完成！";
                MessageBox.Show("火箭发射成功！", "发射完成",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                pictureBox1.Left = this.ClientSize.Width / 2 - pictureBox1.Width / 2;
            }
        }
    }
}